//
//  MessageVC.m
//  lixing
//
//  Created by 谭明星 on 2021/12/19.
//

#import "LXSysMessageViewController.h"
#import "LXSystemMsgModel.h"
#import "LXMessageListContentCell.h"
#import "LXCarLeaseApplyViewController.h"
#import "LXAccurateFindCarViewController.h"
@interface LXSysMessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic) NSInteger page;
@property (nonatomic) NSMutableArray *data;
@end

@implementation LXSysMessageViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type == 1) {
        [self layoutCustomNav:self.view title:@"系统消息"];
    }
    else if(self.type == 4){
        [self layoutCustomNav:self.view title:@"补车提醒"];
    }
    
    [self layoutUI];
    self.data = [NSMutableArray array];
    self.page = 1;
   
//    [self loadData];
    
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetMessageReadFlag] params:@{@"type":@(self.type),@"userCode":@"1"} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        
    } withShowHud:YES];
    
    
}
- (void)layoutUI
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, ScreenHeight - kViewTopHeight) style:(UITableViewStylePlain)];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = ColorWithF5F5F5;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
//    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageListContentCell class]) bundle:nil]  forCellReuseIdentifier:@"cell1"];
//    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageListContentCell class]) bundle:nil]  forCellReuseIdentifier:@"cell2"];

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];
//
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page++;
        [self loadData];
    }];
    
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}


-(void)loadData{
    
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetMessageList] params:@{@"page":@(self.page),@"rows":@(10),@"type":@(self.type),@"userCode":@"1"} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            
            if (self.page == 1 || [self.tableView.mj_header isRefreshing]) {
                [self.data removeAllObjects];
                self.tableView.mj_header.state = MJRefreshStateIdle;
                [self.tableView.mj_footer resetNoMoreData];
            }
            NSArray *ary = [LXSystemMsgModel mj_objectArrayWithKeyValuesArray:resonseObj[@"list"][@"records"]];
            [self.data addObjectsFromArray:ary];
            if (ary.count < 1) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
            
        }
        
        
        [self endRefreshing];
        [self isHaveEmptyData];
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self isHaveEmptyData];
    } withShowHud:YES];
    
   
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LXSystemMsgModel *model = self.data[indexPath.row];
    
    static NSString *identifier = @"";
    NSInteger index = 0;
    // 资金类型 或者是订单类型
//    if (model.businessType == 2) {
//        identifier = @"cell1";
//        index = 0;
//    }
//    else {
//        identifier = @"cell2";
//        index = 1;
//    }
    if(self.type == 4){
        identifier = @"cell1";
        index = 2;
    }
    else {
        identifier = @"cell2";
       index = 1;
    }

    
    LXMessageListContentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXMessageListContentCell class]) owner:self options:nil] objectAtIndex:index];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (self.type == 4) {
        cell.model3 = model;
    }
    else {
        cell.model2 = model;
    }
    
  
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LXSystemMsgModel *model = self.data[indexPath.row];

    if (model.carTypeFlag == 1) {
     
        showAlert(@"已补车");
        return;
    }
    if (self.type == 4) {
        
        [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetCarTypeDetail] params:@{@"id":model.businessId} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            
            if (isSuccess) {
                LXCarLeaseApplyViewController *vc = [[LXCarLeaseApplyViewController alloc] init];
                NSDictionary *dic = @{
                    @"id":resonseObj[@"carBrandId"],
                    @"carBrand":resonseObj[@"brand"],
                    @"carMode":resonseObj[@"model"],
                    @"runPlatfom":@([resonseObj[@"runPlatfom"] intValue]),
                    @"carType":@([resonseObj[@"carType"] intValue]),
                    @"energeType":@([resonseObj[@"energeType"] intValue]),
                    @"floorPrice":resonseObj[@"floorPrice"],
                    @"endurance":@([resonseObj[@"endurance"] intValue]),
                    @"publishedId":model.businessId,
                };
                
                CheLiangModel *mm = [CheLiangModel mj_objectWithKeyValues:dic];
                vc.carModel = mm;
                vc.type = 3;
                vc.fromVc = NSStringFromClass([self class]);
                [self.navigationController pushViewController:vc animated:YES];
            }
            
            
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];

       
    }

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}
@end
